import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    beforeEnter: (to, from, next) => {
	  const token = localStorage.getItem('token');
	  if (token) {
	    next('/enterprisePosition');
	  } else {
	    next('/login');
	  }
	}
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/login.vue'),
    meta: {
      name: "登录"
    },
    beforeEnter: (to, from, next) => {
      const token = localStorage.getItem('token');
      if (token) {
        next('/index');
      } else {
        next();
      }
    }
  },
  // {
  //   path: '/eEditPersonal',
  //   component: () => import('../views/editPersonal/eEditPersonal.vue'),
  //   meta: {
  //     name: "账号设置"
  //   }
  // },
  {
    path: '/cInformation',
    component: () => import('../views/cInformation/cInformation.vue'),
    meta: {
      name: "法人信息"
    }
  },
  {
    path: '/editInfo',
    component: () => import('../views/editInfo/editInfo.vue'),
    meta: {
      name: "编辑单位基本信息"
    }
  },
  {
    path: '/eInformation',
    component: () => import('../views/eInformation/eInformation.vue'),
    meta: {
      name: "单位信息"
    }
  },
  {
    path: '/postPosition',
    component: () => import('../views/postPosition/postPosition.vue'),
    meta: {
      name: "单位发布职位"
    }
  },
  {
    path: '/enterprisePosition',
    component: () => import('../views/enterprisePosition/enterprisePosition.vue'),
    meta: {
      name: "单位职位"
    }
  },
  {
    path: '/forgotPassword',
    component: () => import('../views/login/forgotPassword.vue'),
    meta: {
      name: "忘记密码"
    }
  },
  {
    path: '/pageNotFound',
    name: 'pageNotFound',
    component: () => import("../views/page-not-found/pagenotfound.vue")
  },
  {
    path: '*',
    redirect: '/pageNotFound',
    name: 'notFound',
    hidden: true
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  }
})

const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}

export default router